package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.smedi.ismedi.authority.starter.util.VerifyHelper;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.ProcessComment;
import com.smedi.ismedi.inmail.core.domain.dto.ProcessCommentQuery;
import com.smedi.ismedi.inmail.core.domain.repository.ProcessCommentRepository;
import com.smedi.ismedi.inmail.core.port.adapter.convert.AdapterConverter;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.ProcessCommentEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.ProcessCommentMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 主体院关系实现类
 *
 * @author niuyanling
 * @date 2021/08/05
 */
@Repository
@AllArgsConstructor
public class ProcessCommentRepositoryImpl implements ProcessCommentRepository {
    private final AdapterConverter adapterConverter;
    private final VerifyHelper verifyHelper;

    private final ProcessCommentMapper processCommentMapper;

    @Override
    public ProcessComment getProcessCommentOne(ProcessCommentQuery processCommentQuery) {
        LambdaQueryWrapper<ProcessCommentEntity> lambdaQueryWrapper = this.ProcessCommentQueryToLambdaQueryWrapper(processCommentQuery);

        ProcessCommentEntity processCommentEntity = processCommentMapper.selectOne(lambdaQueryWrapper);

        return adapterConverter.processCommentEntityToProcessComment(processCommentEntity);
    }

    @Override
    public List<ProcessComment> getProcessCommentList(ProcessCommentQuery processCommentQuery) {
        LambdaQueryWrapper<ProcessCommentEntity> lambdaQueryWrapper = this.ProcessCommentQueryToLambdaQueryWrapper(processCommentQuery);

        List<ProcessCommentEntity> processCommentEntities = processCommentMapper.selectList(lambdaQueryWrapper);

        return adapterConverter.processCommentEntityToProcessComment(processCommentEntities);
    }

    private LambdaQueryWrapper<ProcessCommentEntity> ProcessCommentQueryToLambdaQueryWrapper(ProcessCommentQuery processCommentQuery) {
        LambdaQueryWrapper<ProcessCommentEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processCommentQuery.getId()), ProcessCommentEntity::getId, processCommentQuery.getId());
        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processCommentQuery.getIds()), ProcessCommentEntity::getId, processCommentQuery.getIds());
        lambdaQueryWrapper.eq(verifyHelper.isAvailable(processCommentQuery.getTaskId()), ProcessCommentEntity::getTaskId, processCommentQuery.getTaskId());
        lambdaQueryWrapper.in(verifyHelper.isAvailable(processCommentQuery.getTaskIds()), ProcessCommentEntity::getTaskId, processCommentQuery.getTaskIds());

        return lambdaQueryWrapper;
    }
}